

/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
package SMS;
/**
 * 
 */
class CGSMAlphabet
{
	private static final char[][] grcAlphabetRemapping = { { '\u0386', '\u0041' }, // GREEK
	        // CAPITAL
	        // LETTER
	        // ALPHA
	        // WITH
	        // TONOS
	        { '\u0388', '\u0045' }, // GREEK CAPITAL LETTER EPSILON WITH TONOS
	        { '\u0389', '\u0048' }, // GREEK CAPITAL LETTER ETA WITH TONOS
	        { '\u038A', '\u0049' }, // GREEK CAPITAL LETTER IOTA WITH TONOS
	        { '\u038C', '\u004F' }, // GREEK CAPITAL LETTER OMICRON WITH TONOS
	        { '\u038E', '\u0059' }, // GREEK CAPITAL LETTER UPSILON WITH TONOS
	        { '\u038F', '\u03A9' }, // GREEK CAPITAL LETTER OMEGA WITH TONOS
	        { '\u0390', '\u0049' }, // GREEK SMALL LETTER IOTA WITH DIALYTIKA
	        // AND TONOS
	        { '\u0391', '\u0041' }, // GREEK CAPITAL LETTER ALPHA
	        { '\u0392', '\u0042' }, // GREEK CAPITAL LETTER BETA
	        { '\u0393', '\u0393' }, // GREEK CAPITAL LETTER GAMMA
	        { '\u0394', '\u0394' }, // GREEK CAPITAL LETTER DELTA
	        { '\u0395', '\u0045' }, // GREEK CAPITAL LETTER EPSILON
	        { '\u0396', '\u005A' }, // GREEK CAPITAL LETTER ZETA
	        { '\u0397', '\u0048' }, // GREEK CAPITAL LETTER ETA
	        { '\u0398', '\u0398' }, // GREEK CAPITAL LETTER THETA
	        { '\u0399', '\u0049' }, // GREEK CAPITAL LETTER IOTA
	        { '\u039A', '\u004B' }, // GREEK CAPITAL LETTER KAPPA
	        { '\u039B', '\u039B' }, // GREEK CAPITAL LETTER LAMDA
	        { '\u039C', '\u004D' }, // GREEK CAPITAL LETTER MU
	        { '\u039D', '\u004E' }, // GREEK CAPITAL LETTER NU
	        { '\u039E', '\u039E' }, // GREEK CAPITAL LETTER XI
	        { '\u039F', '\u004F' }, // GREEK CAPITAL LETTER OMICRON
	        { '\u03A0', '\u03A0' }, // GREEK CAPITAL LETTER PI
	        { '\u03A1', '\u0050' }, // GREEK CAPITAL LETTER RHO
	        { '\u03A3', '\u03A3' }, // GREEK CAPITAL LETTER SIGMA
	        { '\u03A4', '\u0054' }, // GREEK CAPITAL LETTER TAU
	        { '\u03A5', '\u0059' }, // GREEK CAPITAL LETTER UPSILON
	        { '\u03A6', '\u03A6' }, // GREEK CAPITAL LETTER PHI
	        { '\u03A7', '\u0058' }, // GREEK CAPITAL LETTER CHI
	        { '\u03A8', '\u03A8' }, // GREEK CAPITAL LETTER PSI
	        { '\u03A9', '\u03A9' }, // GREEK CAPITAL LETTER OMEGA
	        { '\u03AA', '\u0049' }, // GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
	        { '\u03AB', '\u0059' }, // GREEK CAPITAL LETTER UPSILON WITH
	        // DIALYTIKA
	        { '\u03AC', '\u0041' }, // GREEK SMALL LETTER ALPHA WITH TONOS
	        { '\u03AD', '\u0045' }, // GREEK SMALL LETTER EPSILON WITH TONOS
	        { '\u03AE', '\u0048' }, // GREEK SMALL LETTER ETA WITH TONOS
	        { '\u03AF', '\u0049' }, // GREEK SMALL LETTER IOTA WITH TONOS
	        { '\u03B0', '\u0059' }, // GREEK SMALL LETTER UPSILON WITH DIALYTIKA
	        // AND TONOS
	        { '\u03B1', '\u0041' }, // GREEK SMALL LETTER ALPHA
	        { '\u03B2', '\u0042' }, // GREEK SMALL LETTER BETA
	        { '\u03B3', '\u0393' }, // GREEK SMALL LETTER GAMMA
	        { '\u03B4', '\u0394' }, // GREEK SMALL LETTER DELTA
	        { '\u03B5', '\u0045' }, // GREEK SMALL LETTER EPSILON
	        { '\u03B6', '\u005A' }, // GREEK SMALL LETTER ZETA
	        { '\u03B7', '\u0048' }, // GREEK SMALL LETTER ETA
	        { '\u03B8', '\u0398' }, // GREEK SMALL LETTER THETA
	        { '\u03B9', '\u0049' }, // GREEK SMALL LETTER IOTA
	        { '\u03BA', '\u004B' }, // GREEK SMALL LETTER KAPPA
	        { '\u03BB', '\u039B' }, // GREEK SMALL LETTER LAMDA
	        { '\u03BC', '\u004D' }, // GREEK SMALL LETTER MU
	        { '\u03BD', '\u004E' }, // GREEK SMALL LETTER NU
	        { '\u03BE', '\u039E' }, // GREEK SMALL LETTER XI
	        { '\u03BF', '\u004F' }, // GREEK SMALL LETTER OMICRON
	        { '\u03C0', '\u03A0' }, // GREEK SMALL LETTER PI
	        { '\u03C1', '\u0050' }, // GREEK SMALL LETTER RHO
	        { '\u03C2', '\u03A3' }, // GREEK SMALL LETTER FINAL SIGMA
	        { '\u03C3', '\u03A3' }, // GREEK SMALL LETTER SIGMA
	        { '\u03C4', '\u0054' }, // GREEK SMALL LETTER TAU
	        { '\u03C5', '\u0059' }, // GREEK SMALL LETTER UPSILON
	        { '\u03C6', '\u03A6' }, // GREEK SMALL LETTER PHI
	        { '\u03C7', '\u0058' }, // GREEK SMALL LETTER CHI
	        { '\u03C8', '\u03A8' }, // GREEK SMALL LETTER PSI
	        { '\u03C9', '\u03A9' }, // GREEK SMALL LETTER OMEGA
	        { '\u03CA', '\u0049' }, // GREEK SMALL LETTER IOTA WITH DIALYTIKA
	        { '\u03CB', '\u0059' }, // GREEK SMALL LETTER UPSILON WITH DIALYTIKA
	        { '\u03CC', '\u004F' }, // GREEK SMALL LETTER OMICRON WITH TONOS
	        { '\u03CD', '\u0059' }, // GREEK SMALL LETTER UPSILON WITH TONOS
	        { '\u03CE', '\u03A9' } // GREEK SMALL LETTER OMEGA WITH TONOS
	};

	private static final char[] extAlphabet = { '\u000c', // FORM FEED
	        '\u005e', // CIRCUMFLEX ACCENT
	        '\u007b', // LEFT CURLY BRACKET
	        '\u007d', // RIGHT CURLY BRACKET
	        '\\', // REVERSE SOLIDUS
	        '\u005b', // LEFT SQUARE BRACKET
	        '\u007e', // TILDE
	        '\u005d', // RIGHT SQUARE BRACKET
	        '\u007c', // VERTICAL LINES
	        '\u20ac', // EURO SIGN
	};

	private static final String[] extBytes = { "1b0a", // FORM FEED
	        "1b14", // CIRCUMFLEX ACCENT
	        "1b28", // LEFT CURLY BRACKET
	        "1b29", // RIGHT CURLY BRACKET
	        "1b2f", // REVERSE SOLIDUS
	        "1b3c", // LEFT SQUARE BRACKET
	        "1b3d", // TILDE
	        "1b3e", // RIGHT SQUARE BRACKET
	        "1b40", // VERTICAL LINES
	        "1b65", // EURO SIGN
	};

	private static final char[] stdAlphabet = { '\u0040', // COMMERCIAL AT
	        '\u00A3', // POUND SIGN
	        '\u0024', // DOLLAR SIGN
	        '\u00A5', // YEN SIGN
	        '\u00E8', // LATIN SMALL LETTER E WITH GRAVE
	        '\u00E9', // LATIN SMALL LETTER E WITH ACUTE
	        '\u00F9', // LATIN SMALL LETTER U WITH GRAVE
	        '\u00EC', // LATIN SMALL LETTER I WITH GRAVE
	        '\u00F2', // LATIN SMALL LETTER O WITH GRAVE
	        '\u00E7', // LATIN SMALL LETTER C WITH CEDILLA
	        '\n', // LINE FEED
	        '\u00D8', // LATIN CAPITAL LETTER O WITH STROKE
	        '\u00F8', // LATIN SMALL LETTER O WITH STROKE
	        '\r', // CARRIAGE RETURN
	        '\u00C5', // LATIN CAPITAL LETTER A WITH RING ABOVE
	        '\u00E5', // LATIN SMALL LETTER A WITH RING ABOVE
	        '\u0394', // GREEK CAPITAL LETTER DELTA
	        '\u005F', // LOW LINE
	        '\u03A6', // GREEK CAPITAL LETTER PHI
	        '\u0393', // GREEK CAPITAL LETTER GAMMA
	        '\u039B', // GREEK CAPITAL LETTER LAMDA
	        '\u03A9', // GREEK CAPITAL LETTER OMEGA
	        '\u03A0', // GREEK CAPITAL LETTER PI
	        '\u03A8', // GREEK CAPITAL LETTER PSI
	        '\u03A3', // GREEK CAPITAL LETTER SIGMA
	        '\u0398', // GREEK CAPITAL LETTER THETA
	        '\u039E', // GREEK CAPITAL LETTER XI
	        '\u00A0', // ESCAPE TO EXTENSION TABLE (or displayed as NBSP, see
	        // note above)
	        '\u00C6', // LATIN CAPITAL LETTER AE
	        '\u00E6', // LATIN SMALL LETTER AE
	        '\u00DF', // LATIN SMALL LETTER SHARP S (German)
	        '\u00C9', // LATIN CAPITAL LETTER E WITH ACUTE
	        '\u0020', // SPACE
	        '\u0021', // EXCLAMATION MARK
	        '\u0022', // QUOTATION MARK
	        '\u0023', // NUMBER SIGN
	        '\u00A4', // CURRENCY SIGN
	        '\u0025', // PERCENT SIGN
	        '\u0026', // AMPERSAND
	        '\'', // APOSTROPHE
	        '\u0028', // LEFT PARENTHESIS
	        '\u0029', // RIGHT PARENTHESIS
	        '\u002A', // ASTERISK
	        '\u002B', // PLUS SIGN
	        '\u002C', // COMMA
	        '\u002D', // HYPHEN-MINUS
	        '\u002E', // FULL STOP
	        '\u002F', // SOLIDUS
	        '\u0030', // DIGIT ZERO
	        '\u0031', // DIGIT ONE
	        '\u0032', // DIGIT TWO
	        '\u0033', // DIGIT THREE
	        '\u0034', // DIGIT FOUR
	        '\u0035', // DIGIT FIVE
	        '\u0036', // DIGIT SIX
	        '\u0037', // DIGIT SEVEN
	        '\u0038', // DIGIT EIGHT
	        '\u0039', // DIGIT NINE
	        '\u003A', // COLON
	        '\u003B', // SEMICOLON
	        '\u003C', // LESS-THAN SIGN
	        '\u003D', // EQUALS SIGN
	        '\u003E', // GREATER-THAN SIGN
	        '\u003F', // QUESTION MARK
	        '\u00A1', // INVERTED EXCLAMATION MARK
	        '\u0041', // LATIN CAPITAL LETTER A
	        '\u0042', // LATIN CAPITAL LETTER B
	        '\u0043', // LATIN CAPITAL LETTER C
	        '\u0044', // LATIN CAPITAL LETTER D
	        '\u0045', // LATIN CAPITAL LETTER E
	        '\u0046', // LATIN CAPITAL LETTER F
	        '\u0047', // LATIN CAPITAL LETTER G
	        '\u0048', // LATIN CAPITAL LETTER H
	        '\u0049', // LATIN CAPITAL LETTER I
	        '\u004A', // LATIN CAPITAL LETTER J
	        '\u004B', // LATIN CAPITAL LETTER K
	        '\u004C', // LATIN CAPITAL LETTER L
	        '\u004D', // LATIN CAPITAL LETTER M
	        '\u004E', // LATIN CAPITAL LETTER N
	        '\u004F', // LATIN CAPITAL LETTER O
	        '\u0050', // LATIN CAPITAL LETTER P
	        '\u0051', // LATIN CAPITAL LETTER Q
	        '\u0052', // LATIN CAPITAL LETTER R
	        '\u0053', // LATIN CAPITAL LETTER S
	        '\u0054', // LATIN CAPITAL LETTER T
	        '\u0055', // LATIN CAPITAL LETTER U
	        '\u0056', // LATIN CAPITAL LETTER V
	        '\u0057', // LATIN CAPITAL LETTER W
	        '\u0058', // LATIN CAPITAL LETTER X
	        '\u0059', // LATIN CAPITAL LETTER Y
	        '\u005A', // LATIN CAPITAL LETTER Z
	        '\u00C4', // LATIN CAPITAL LETTER A WITH DIAERESIS
	        '\u00D6', // LATIN CAPITAL LETTER O WITH DIAERESIS
	        '\u00D1', // LATIN CAPITAL LETTER N WITH TILDE
	        '\u00DC', // LATIN CAPITAL LETTER U WITH DIAERESIS
	        '\u00A7', // SECTION SIGN
	        '\u00BF', // INVERTED QUESTION MARK
	        '\u0061', // LATIN SMALL LETTER A
	        '\u0062', // LATIN SMALL LETTER B
	        '\u0063', // LATIN SMALL LETTER C
	        '\u0064', // LATIN SMALL LETTER D
	        '\u0065', // LATIN SMALL LETTER E
	        '\u0066', // LATIN SMALL LETTER F
	        '\u0067', // LATIN SMALL LETTER G
	        '\u0068', // LATIN SMALL LETTER H
	        '\u0069', // LATIN SMALL LETTER I
	        '\u006A', // LATIN SMALL LETTER J
	        '\u006B', // LATIN SMALL LETTER K
	        '\u006C', // LATIN SMALL LETTER L
	        '\u006D', // LATIN SMALL LETTER M
	        '\u006E', // LATIN SMALL LETTER N
	        '\u006F', // LATIN SMALL LETTER O
	        '\u0070', // LATIN SMALL LETTER P
	        '\u0071', // LATIN SMALL LETTER Q
	        '\u0072', // LATIN SMALL LETTER R
	        '\u0073', // LATIN SMALL LETTER S
	        '\u0074', // LATIN SMALL LETTER T
	        '\u0075', // LATIN SMALL LETTER U
	        '\u0076', // LATIN SMALL LETTER V
	        '\u0077', // LATIN SMALL LETTER W
	        '\u0078', // LATIN SMALL LETTER X
	        '\u0079', // LATIN SMALL LETTER Y
	        '\u007A', // LATIN SMALL LETTER Z
	        '\u00E4', // LATIN SMALL LETTER A WITH DIAERESIS
	        '\u00F6', // LATIN SMALL LETTER O WITH DIAERESIS
	        '\u00F1', // LATIN SMALL LETTER N WITH TILDE
	        '\u00FC', // LATIN SMALL LETTER U WITH DIAERESIS
	        '\u00E0', // LATIN SMALL LETTER A WITH GRAVE
	};
        
        /** Converts the byte array to the string */
	public static String bytesToString(byte[] bytes)
	{
		StringBuffer text;
		String extChar;
		int i, j;

		text = new StringBuffer();
		for (i = 0; i < bytes.length; i++)
		{
			if (bytes[i] == 0x1b)
			{
				extChar = "1b" + Integer.toHexString(bytes[++i]);
				for (j = 0; j < extBytes.length; j++)
					if (extBytes[j].equalsIgnoreCase(extChar)) text.append(extAlphabet[j]);
			}
			else text.append(stdAlphabet[bytes[i]]);
		}
		return text.toString();
	}
        
        /** Converts the string to a byte array */
	public static int stringToBytes(String text, byte[] bytes)
	{
		int i, j, k, index;
		char ch;

		k = 0;
		for (i = 0; i < text.length(); i++)
		{
			ch = text.charAt(i);
			index = -1;
			for (j = 0; j < extAlphabet.length; j++)
				if (extAlphabet[j] == ch)
				{
					index = j;
					break;
				}
			if (index != -1) // An extended char...
			{
				bytes[k] = (byte) Integer.parseInt(extBytes[index].substring(0, 2), 16);
				k++;
				bytes[k] = (byte) Integer.parseInt(extBytes[index].substring(2, 4), 16);
				k++;
			}
			else
			// Maybe a standard char...
			{
				index = -1;
				for (j = 0; j < stdAlphabet.length; j++)
					if (stdAlphabet[j] == ch)
					{
						index = j;
						bytes[k] = (byte) j;
						k++;
						break;
					}
				if (index == -1) // Maybe a Greek Char...
				{
					for (j = 0; j < grcAlphabetRemapping.length; j++)
						if (grcAlphabetRemapping[j][0] == ch)
						{
							index = j;
							ch = grcAlphabetRemapping[j][1];
							break;
						}
					if (index != -1)
					{
						for (j = 0; j < stdAlphabet.length; j++)
							if (stdAlphabet[j] == ch)
							{
								index = j;
								bytes[k] = (byte) j;
								k++;
								break;
							}
					}
					else
					// Unknown char replacement...
					{
						bytes[k] = (byte) ' ';
						k++;
					}
				}
			}
		}
		return k;
	}

}
